package com.tanhua.mongo.api;

import com.tanhua.api.UserLocationApi;
import com.tanhua.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //上报地理位置   每个用户只有一个地理位置数据
    @Override
    public boolean saveAndUpdate(Long userId, Double longitude, Double latitude, String address) {
        try {
            //1 封装查询条件
            Query query = Query.query(Criteria.where("userId").is(userId));
            GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude, latitude);

            //2 判断数据是否存在
            if (mongoTemplate.exists(query, UserLocation.class)) {
                //存在  更新
                Update update = Update.update("location", geoJsonPoint)
                        .set("address", address)
                        .set("updated", System.currentTimeMillis())
                        .set("lastUpdated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLocation.class);

            } else {
                //不存在 新增
                UserLocation userLocation = new UserLocation();
                userLocation.setUserId(userId);
                userLocation.setLocation(geoJsonPoint);
                userLocation.setAddress(address);
                userLocation.setCreated(System.currentTimeMillis());
                userLocation.setUpdated(System.currentTimeMillis());
                userLocation.setLastUpdated(System.currentTimeMillis());
                mongoTemplate.save(userLocation);

            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //根据用户id  和距离查询附近的人
    @Override
    public List<Long> queryNearUser(Long userId, double distance) {
        //查询用户的定位信息
        Query queryUserLocation = Query.query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(queryUserLocation, UserLocation.class);

        //构造坐标点
        GeoJsonPoint point = userLocation.getLocation();
        //构造半径
        Distance distanceObj = new Distance(distance / 1000, Metrics.KILOMETERS);
        //画了一个圆圈
        Circle circle = new Circle(point, distanceObj);
        //构造query对象
        Query query = Query.query(Criteria.where("location").withinSphere(circle));
        //省略其他内容
        List<UserLocation> list = mongoTemplate.find(query, UserLocation.class);

        List<Long> ids = list.stream()
                .map(UserLocation::getUserId).collect(Collectors.toList());

        return ids;
    }
}
